package liziy.spring.beans.support;

import liziy.spring.beans.factory.config.BeanDefinition;
import liziy.spring.beans.BeanDefinitionRegistry;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Liziy
 * @date 2020/12/15 23:36
 **/
public class DefaultListableBeanFactory extends AbstractBeanFactory implements BeanDefinitionRegistry {
    //创建一个容器用于注册beanName和beanDefinition对象集合
    private Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(32);

    /**
     * 注册需要被容器管理控制别名称与bean定义
     * @param beanName
     * @param definition
     * @return
     */
    @Override
    public int registryBeanDefinition(String beanName, BeanDefinition definition) {
        synchronized (beanDefinitionMap){
            if (!beanDefinitionMap.containsKey(beanName)){
                this.beanDefinitionMap.put(beanName, definition);
            }
        }
        return 0;
    }

    @Override
    protected BeanDefinition getBeanDefinition(String beanName) {
        return this.beanDefinitionMap.get(beanName);
    }
}
